#! /bin/sh
#
# mountvirtfs	Mount all the virtual filesystems the kernel
#		provides and that are required by default.
#
#		This script can be called several times without
#		damage; it tries to mount the virtual filesystems
#		only if not mounted yet, and only updates /etc/mtab
#		if it is writable and there is a need to.
#
#		This functionality was previously provided by
#		mountkernfs from the glibc package.
#
# Version:	@(#)mountvirtfs  2.85-23  29-Jul-2004  miquels
#
# chkconfig: S 2 0
#

# Script needs to be robust and continue when parts fail,
# so we're not setting the "-e" flag.
#set -e

PATH=/lib/init:/bin:/sbin:/usr/bin:/usr/sbin

TTYGRP=5
TTYMODE=620
if [ -f /etc/default/devpts ]
then
	. /etc/default/devpts
fi

TMPFS_SIZE=
if [ -f /etc/default/tmpfs ]
then
	. /etc/default/tmpfs
fi

KERNEL=`uname -s`
umask 022

dir_writable () {
	if [ -d "$1/" ] && [ -w "$1/" ] && touch "$1/test" 
	then
		rm $1/test
		return 0
	fi
	return 1
}

domount () {

	# Directory present ?
	if [ ! -d $3 ]
	then
		return
	fi

	# Do we support this filesystem type ?
	TYPE=
	if [ $1 = proc ]
	then
		case "$KERNEL" in
			Linux|GNU)
				TYPE=proc
				;;
			*)
				TYPE=procfs
				;;
		esac
	elif grep -E -qs "$1\$" /proc/filesystems
	then
		TYPE=$1
	elif grep -E -qs "$2\$" /proc/filesystems
	then
		TYPE=$2
	fi
	if [ "$TYPE" = "" ]
	then
		return
	fi

	#
	#	Get the options from /etc/fstab.
	#
	OPTS=
	if [ -f /etc/fstab ]
	then
		exec 9<&0 0</etc/fstab
		while read FDEV FDIR FTYPE FOPTS REST
		do
			case "$FDEV" in
				""|\#*)
					continue
					;;
			esac
			if [ "$3" != "$FDIR" ] || [ "$TYPE" != "$FTYPE" ]
			then
				continue
			fi
			case "$FOPTS" in
				noauto|*,noauto|noauto,*|*,noauto,*)
					return
					;;
				?*)
					OPTS="-o$FOPTS"
					;;
			esac
			break
		done
		exec 0<&9 9<&-
	fi

	# See if anything is mounted yet
	if ! mountpoint -q $3
	then
		# No, do it now
		mount $MOUNT_N -t $TYPE $OPTS $4 $TYPE $3
	else
		# Need to update mtab only ?
		if [ -n "$DO_MTAB" ] &&
		   ! grep -E -sq "^([^ ]+) +$3 +" /etc/mtab
		then
				mount -f -t $TYPE $OPTS $4 $TYPE $3
		fi
	fi
}

#
#	We only create/modify /etc/mtab if the location where it is
#	stored is writable.  If /etc/mtab is a symlink into /proc/
#	then it is not writable.
#
DO_MTAB=
MOUNT_N=-n
MTAB_PATH="`readlink -f /etc/mtab || :`"
case "$MTAB_PATH" in
	/proc/*)
		;;
	/*)
		if dir_writable ${MTAB_PATH%/*}
		then
			DO_MTAB=Yes
			MOUNT_N=
		fi
		;;
esac

if [ -n "$DO_MTAB" ] && [ ! -f /etc/mtab ]
then
	:> /etc/mtab
fi

# Mount standard /proc and /sys.
domount proc "" /proc
domount sysfs "" /sys

# added by willi
#BEGIN

# Remount with atime and nodiratime for ext2/ext3 root filesystems
#grep -q "/dev/sd[a-d][1-9] / ext[2|3]" /proc/mounts && mount $MOUNT_N / -o errors=remount-ro,noatime,nodiratime,ro,remount

domakedev() {
        cd $1
	ln -sf ../../sbin/MAKEDEV MAKEDEV
	mknod -m 0666 fuse c 10 229
	mknod -m 0666 avs c 221 0
	mknod -m 0666 /dev/hdmi0.0 c 254 0
        MAKEDEV="./MAKEDEV -p /etc/passwd -g /etc/group"
        ${MAKEDEV} std
        ${MAKEDEV} fd
        ##${MAKEDEV} fd0 fd1
        ${MAKEDEV} hda hdb
        ${MAKEDEV} sda sdb sdc sdd
        ${MAKEDEV} scd0 scd1
        ${MAKEDEV} st0 st1
        ${MAKEDEV} sg
        ${MAKEDEV} ptyp ptyq
        ${MAKEDEV} console
        ##${MAKEDEV} ttyS0 ttyS1 ttyS2 ttyS3 ttyS4
        ##${MAKEDEV} ttySC0 ttySC1 ttySC2 ttySC3 ttySC4
        ##${MAKEDEV} ttyFW0 ttyFW1 ttyFW2 ttyFW3
        ${MAKEDEV} ttyAS0 ttyAS1 ttyAS2 ttyAS3
        ${MAKEDEV} lp par audio video fb rtc lirc st200 alsasnd
        ${MAKEDEV} ppp busmice
        ln -s -f /dev/input/mouse0 mouse
        ${MAKEDEV} input i2c mtd
	${MAKEDEV} dvb
        #${MAKEDEV} ide0 ide1
        #${MAKEDEV} sound

        ##rm -rf pts
        mkdir pts
        ##chown root.root pts
        ##chmod 0755 pts
        ##rm -rf shm
        mkdir shm
        ##chown root.root shm
        ##chmod 0755 shm
}

doramdev() {
	RAMMOUNT="/ram"
	EXTRA_PARAMS=""
	#FLASH_MAX=92m
	dir="/dev"
	ramdir=$RAMMOUNT$dir
	if [ "xx$FLASH_MAX" != "xx" ]; then
		EXTRA_PARAMS=" -o size=$FLASH_MAX "
	fi
	grep -q "/ram" /proc/mounts || mount -t tmpfs tmpfs $RAMMOUNT $EXTRA_PARAMS

	if [ ! -d $ramdir ]; then
		cp -a $dir $ramdir
	fi

	#mkdir -p $ramdir.flash
	#mount --bind $dir $ramdir.flash
	mount --bind $dir /dev.static
	mount --bind $ramdir $dir
}

# Workaround for static /dev population in ram
#

# Check for /dev in tmpfs (indicates pre-init mechanism)
#
#echo "mountvirtfs: "
#cat /proc/mounts
#echo
if grep -q "/dev tmpfs" /proc/mounts; then
	if [ -x /sbin/udevd ]; then
		:
	else
		if grep -q "mini_fo" /proc/mounts; then
			#devstatic="/ram/dev.flash"
			devstatic="/dev.static"

			#egrep "/dev/root /( |rom )nfs" /proc/mounts
			#"/( ?|(rom)) "
			if  grep -q "/dev/root /rom nfs" /proc/mounts && [ ! -d ${devstatic}/pts ]; then
				# Fill initially ${devstatic} (nfs)
				#
				mount /rom -o rw,remount
       	 			echo "Making devices in ${devstatic}..."
				domakedev ${devstatic} && \
					mount /rom -o ro,remount
			fi
	    if  grep -q "/dev/root /rom ext2" /proc/mounts && [ ! -d ${devstatic}/pts ]; then
				# Fill initially ${devstatic} (usb)
				#
				mount /rom -o rw,remount
       	 			echo "Making devices in ${devstatic}..."
				domakedev ${devstatic} && \
					mount /rom -o ro,remount
			fi 
			# Copy ${devstatic}/* to /dev tmpfs
			echo "Copying device nodes to /dev"
			cp -a ${devstatic}/* /dev/

		else
			#devstatic="/ram/dev.flash"
			devstatic="/dev.static"

  		if grep -q "/dev/root / nfs" /proc/mounts && [ ! -d ${devstatic}/pts ]; then
				# Fill initially ${devstatic} (nfs)
				#
       	 			echo "Making devices in ${devstatic}..."
			  domakedev ${devstatic}
			fi
			if grep -q "/dev/root / ext2" /proc/mounts && [ ! -d ${devstatic}/pts ]; then
				# Fill initially ${devstatic} (usb)
				#
       	 			echo "Making devices in ${devstatic}..."
			  domakedev ${devstatic}
			fi
	
			# Copy ${devstatic}/* to /dev tmpfs
			echo "Copying device nodes to /dev"
			cp -a ${devstatic}/* /dev/
		fi
		umount /rom/ram
	fi
else
	if [ -x /sbin/udevd ]; then
		:
	else
  	if grep -q "/dev/root / nfs" /proc/mounts && [ ! -d /dev/pts ]; then
			# Fill initially /dev (nfs)
			#
        		echo "Seems to be the first run - Making devices in /dev..."
			domakedev /dev
		fi
		if grep -q "/dev/root / ext[23]" /proc/mounts && [ ! -d /dev/pts ]; then
			# Fill initially /dev (usb)
			#
        		echo "Seems to be the first run - Making devices in /dev..."
			domakedev /dev
		fi

		# Copy/Mount /dev in tmpfs
		#
		echo "Mount /dev in tmpfs"
		echo "Copying device nodes to /dev"
		doramdev
	fi
fi

#END

# Mount /dev/pts. Create master ptmx node if needed.
#
# As of 2.5.68, devpts is not automounted when using devfs. So we
# mount devpts if it is compiled in (older devfs didn't require it
# to be compiled in at all).
#
if [ "$KERNEL" = Linux ]
then
	#
	#	Since kernel 2.5.something, devfs doesn't include
	#	a standard /dev/pts directory anymore. So if devfs
	#	is mounted on /dev we need to create that directory
	#	manually.
	#
	if grep -qs '/dev devfs' /proc/mounts
	then
		if [ ! -d /dev/pts ]
		then
			mkdir /dev/pts
		fi
	fi
	if [ -d /dev/pts ]
	then
		if dir_writable /dev && [ ! -c /dev/ptmx ]
		then
			mknod -m666 /dev/ptmx c 5 2
		fi
		domount devpts "" /dev/pts -ogid=$TTYGRP,mode=$TTYMODE
	fi
fi

# Mount tmpfs.
#
# Around kernel version 2.3.3x, a memory based filesystem was
# introduced to support POSIX shared memory, called shmfs. 
# Later this filesystem was extended for general usage -
# provided you set the CONFIG_TMPFS compile option and mount
# it as type tmpfs.
#
# Early in the 2.4 kernel series, shmfs was renamed to tmpfs, but
# you could mount it using both type shmfs and tmpfs. Starting
# at kernel version 2.5.44, the shmfs alias was dropped.
#
# Confusingly, in kernels 2.3.x - 2.5.43 where both shmfs and
# tmpfs are present, disabling CONFIG_TMPFS actually removes
# support for shmfs, but tmpfs is still listed in /proc/filesystems
# to support SYSV and POSIX shared memory, and it should still be
# mounted under /dev/shm.
#
# Recommendation: always enable CONFIG_TMPFS and always mount
# using the tmpfs type. Forget about shmfs.
# 
# Tmpfs can be used as memory filesystem, so you can limit tmpfs
# max size using /etc/default/tmpfs to prevent tmpfs from using
# up all system memory.
#
if [ -n "$TMPFS_SIZE" ]
then
	tmpfs_opt="-osize=${TMPFS_SIZE}"
fi
domount tmpfs shmfs /dev/shm $tmpfs_opt

# Mount usbfs/usbdevfs if /proc/bus/usb is present.
#
# Usbfs/usbdevfs is used for USB related binaries/libraries.
# "usbfs" and "usbdevfs" are the exact same filesystem.
# "usbdevfs" was renamed to "usbfs" by linux usb developers,
# because people sometimes mistook it as a part of devfs. Usbfs
# will be superseded by other filesystems (e.g. sysfs), and when
# it becomes obsolete the mount action below should be removed.
#
if [ -d /proc/bus/usb ]
then
	domount usbfs usbdevfs /proc/bus/usb
fi

# 
exit

